package org.r.tools.response;

import com.fasterxml.jackson.annotation.JsonInclude;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import lombok.experimental.Accessors;
import org.r.tools.code.ResCode;

import java.io.Serializable;
import java.util.List;

/**
 * 功能描述
 *
 * @author: R
 * @date: 2022年08月31日 9:16
 */
@Data
@Accessors(chain = true)
@Schema(description = "分页查询返回结果")
@JsonInclude(JsonInclude.Include.NON_NULL)
public class ResPage<T extends Serializable> implements Serializable {

    @Schema(description = "状态码")
    private String code;

    @Schema(description = "提示信息")
    private String message;

    @Schema(description = "数据集合")
    private List<T> records;

    @Schema(description = "当前页")
    private long currentPage;

    @Schema(description = "每页显示数量")
    private long pageSize;

    @Schema(description = "总条数")
    private long totalSize;

    @Schema(description = "总页码")
    private long totalPage;


    private ResPage() {
        this.code = ResCode.SUCCESS;
        this.message = "SUCCESS";
    }

    private ResPage(String code, String msg) {
        this.setCode(code);
        this.setMessage(msg);
    }

    private ResPage(List<T> records) {
        this.code = ResCode.SUCCESS;
        this.message = "SUCCESS";
        this.records = records;
    }


    public static <T extends Serializable> ResPage<T> ok(List<T> records){
        return new ResPage<>(records);
    }

    public static <T extends Serializable> ResPage<T> ok(){
        return new ResPage<>();
    }

    public static <T extends Serializable>ResPage<T> failed(String msg) {
        return new ResPage<>(ResCode.DEFAULT_ERROR, msg);
    }

    public static <T extends Serializable> ResPage<T> failed() {
        return new ResPage<T>(ResCode.DEFAULT_ERROR, "FAILURE");
    }


    /**
     * @param currentPage
     * @param pageSize
     * @param totalSize
     * @param totalPage
     */
    public void setPage(long currentPage, long pageSize, long totalSize, long totalPage) {
        this.currentPage = currentPage;
        this.pageSize = pageSize;
        this.totalSize = totalSize;
        this.totalPage = totalPage;
    }


}

